package com.hellloop.pim.alphabeat.network;

import java.util.ArrayList;
import java.util.List;

import com.hellloop.pim.alphabeat.entity.Sentence;
import com.hellloop.pim.alphabeat.network.PIMMessage.MsgType;

public class PIMClientMessageHandler extends Thread {

	private PIMClient pClient;
	private List<PIMMessage> recMessageQueue;

	public PIMClientMessageHandler(PIMClient client,
			List<PIMMessage> recMessageQueue) {

		/***********************************************************************
		 * Verify input parameter
		 **********************************************************************/

		// Verify that the client is not null
		if (client == null) {
			throw new IllegalArgumentException("PIMClient cannot be null");
		}

		// Verify that the receive message queue is not null
		if (recMessageQueue == null) {
			throw new IllegalArgumentException(
					"Receive Message Queue cannot be null");
		}

		/***********************************************************************
		 * Initiate the required receive objects
		 **********************************************************************/
		this.pClient = client;
		this.recMessageQueue = recMessageQueue;

		/***********************************************************************
		 * Start running
		 **********************************************************************/
		this.start();

	}

	public void run() {

		while (true) {

			if (recMessageQueue.size() > 0) {
				PIMMessage pmsg = recMessageQueue.remove(0);
				processMessage(pmsg);
			}
			try {
				Thread.sleep(1000);
			} catch (Exception ex) {
			}
		}

	}

	private void processMessage(PIMMessage msg) {
		if (msg == null)
			return;
		System.out.println("receiveMessage from server: " + msg.toString());

		try {
			if (msg.getMsgType() == MsgType.ConnectionMsg) {
				PIMConnectionMsg pimMsg = (PIMConnectionMsg) msg;// new
																	// PIMConnectionMsg(
																	// msg.getOprType(),
																	// msg.toString()
																	// );

				if (pimMsg.getOprType() == PIMConnectionMsg.LoginResponse) {
					pClient.doConnectSuccess(Integer.parseInt(pimMsg.getUserId()));
				} else if (pimMsg.getOprType() == PIMConnectionMsg.Disconnect) {

				}
			} else if (msg.getMsgType() == MsgType.RoomMsg) {
				PIMRoomMsg pimMsg = (PIMRoomMsg) msg;

				if (pimMsg.getOprType() == PIMRoomMsg.CreateRoomResponse) {
					pClient.doCreateRoomSuccess(Integer.parseInt(pimMsg
							.getRoomId()));
				} else if (pimMsg.getOprType() == PIMRoomMsg.JoinRoomResponse) {
					pClient.doJoinRoomSuccess(pimMsg.getRoomProp(), pimMsg.getPlayerList());
				}
			} else if (msg.getMsgType() == MsgType.GameMsg) {
				PIMGameMsg pimMsg = (PIMGameMsg) msg;

				if (pimMsg.getOprType() == PIMGameMsg.UserListResponse) {

				} else if (pimMsg.getOprType() == PIMGameMsg.SentenceResponse) {
					String senString = pimMsg.getSentences();
					// TODO Hardcode
					String[] senStringArray = senString.split(":");
					Sentence[] sentenceArray = new Sentence[senStringArray.length];
					for (int i = 0; i < senStringArray.length; i++) {
						// TODO Hardcode
						sentenceArray[i] = new Sentence(senStringArray[i], 10,
								10, 10);
					}
					pClient.doReceiveSentenceListSuccess(sentenceArray);

				}
			}
		} catch (Exception ex) {
			System.out.println(ex);
		}
	}

	/*
	 * // process for Dummy Yuu private void processMessage(PIMMessage pmsg){
	 *  // TODO Do some thing System.out.println("receiveMessage from server: " +
	 * pmsg.getMessage());
	 * 
	 * String cmd = pmsg.getCommand();
	 * 
	 * System.out.println("cmd = " + cmd);
	 *  // Command MUX if(cmd.equals("loginSuccess")) {
	 * 
	 * String[] opers = pmsg.getOperands(); System.out.println("opers = " +
	 * opers);
	 * 
	 * if(opers.length == 2){ String playerName = ""; int id = -1;
	 * 
	 * if(opers[0] == null) { // TODO Error Handling } else { playerName =
	 * opers[0]; }
	 * 
	 * if(opers[1] == null || !opers[1].matches("\\d+")){ // TODO Error Handling }
	 * else { id = Integer.parseInt(opers[1]); }
	 * 
	 * pClient.doConnectSuccess(playerName, id);
	 *  } else { // TODO Error Handling }
	 *  } else if (cmd.equals("createRoomSuccess")) {
	 * 
	 * String[] opers = pmsg.getOperands(); System.out.println("opers = " +
	 * opers);
	 * 
	 * if(opers.length == 1){ int roomId = -1;
	 * 
	 * if(opers[0] == null || !opers[0].matches("\\d+")){ // TODO Error Handling }
	 * else { roomId = Integer.parseInt(opers[0]); }
	 * 
	 * pClient.doCreateRoomSuccess(roomId);
	 *  } else { // TODO Error Handling }
	 *  } else if (cmd.equals("sentenceList")) {
	 * 
	 * String[] opers = pmsg.getOperands(); System.out.println("opers = " +
	 * opers);
	 * 
	 * if(opers.length > 1){
	 * 
	 * List<Sentence> sentenceList = new ArrayList<Sentence>();
	 * 
	 * for(int i = 0; i < opers.length; i++){
	 * 
	 * if(opers[i] == null || opers[i].trim().length() == 0){ // TODO // Error
	 * Handling when sentence is null // or sentence is an empty space } else {
	 * sentenceList.add(new Sentence(opers[i], 20, 1, 1)); }
	 *  }
	 * 
	 * pClient.doReceiveSentenceListSuccess(sentenceList.toArray(new
	 * Sentence[0]));
	 *  } else { // TODO Error Handling }
	 *  } else if (cmd.equals("")) {
	 *  }
	 *  }
	 */

}
